var Section,Contactus,Technical,Products,Gallery,Projects,Banner,Menu,About,Gallas;
var win;
var _show="main";
var intro_button;
var section,contactus,technical,products,gallery,projects,banner,menu,about;
var _ignoreScroll=false;
var console=console?console:{
    log:function(){}
};

onInit=function(){
    Section=Objs("com.stilva.jwi.section.Section");
    // Menu=Objs("com.stilva.jwi.utils.Menu");
    // Gallas=Objs("com.stilva.jwi.section.Gallas");
    // Contactus=Objs("com.stilva.jwi.section.Contactus");
    Banner=Objs("com.stilva.jwi.section.Banner");
    // About=Objs("com.stilva.jwi.section.About");
    // Projects=Objs("com.stilva.jwi.section.Projects");
    // Gallery=Objs("com.stilva.jwi.section.Gallery");
    // Products=Objs("com.stilva.jwi.section.Products");
    // Technical=Objs("com.stilva.jwi.section.Technical");
    // $.address.state("/");
    // intro_button=$("#intro-button");
    // intro_button.bind("click",onIntroButtonHandler);
    section=new Section("section");
    // menu=new Menu();
    // contactus=new Contactus("contact-us");
     banner=new Banner("main",jQuery("section#main"));
	 banner.init();
	 banner.show()
    // about=new About("about",$("section#about"));
    // projects=new Projects("project");
    // gallery=new Gallery("gallery",_imgs);
    // products=new Products("products");
    // technical=new Technical("technical");
    // win=$(window);
    // win.bind("scroll",onScrollHandler);
    // win.bind("resize.intro",onIntroResizeHandler);
    // $(document).bind("mousemove.intro",onIntroMouseHandler);
    // onIntroResizeHandler(null);
    // $("#contact-us-button a").bind("click",function(a){
        // a.preventDefault();
        // contactus.show(0)
        // });
    // menu.init($("div#nav-wrapper"));
    // menu.addMenus("#menu-about","#menu-project","#menu-gallery","#menu-products","#menu-technical");
    // intro_button.find("> a").css({
        // display:"block",
        // opacity:"0"
    // }).animate({
        // opacity:1
    // },400);
    // if(_URL.length>0){
        // onIntroButtonHandler({
            // preventDefault:function(){}
        // })
    // }
};
var Section=Objs("com.stilva.jwi.section.Section",{
    _ID:"",
    minHeight:0,
    yOffset:200,
    self:null,
    sub_selected:null,
    initialize:function(a){
        if(!Section.COLLECTION){
            Section.COLLECTION={};
            
            Section.EVENT_LISTENERS={};
            
            Section.SECTION_HIDDEN="SECTION_HIDDEN";
            Section.SECTION_SHOWN="SECTION_SHOWN"
            }
            this._ID=a;
        Section.COLLECTION[a]=this
        },
    init:function(c,b){
        var a=this;
        this.self=c;
        this.self.find("nav.sub-menu > ul li a").live("click dummyClick",function(d){
            d.preventDefault();
            a._onSubMenuHandler(d)
            });
        Cufon.replace("div.dotted-line a.canvasgroup",{
            fontFamily:"avenir",
            hover:"true"
        })
        }
    trigger:function(e,f){
        var c=Section.EVENT_LISTENERS.ename;
        if(!c||c===undefined){
            return
        }
        var d=0,b=c.length;
        for(d;d<b;d++){
            if(c[d]){
                c[d](f)
                }
            }
        },
bind:function(a,b){
    if(Section.EVENT_LISTENERS.ename){
        Section.EVENT_LISTENERS.ename.push(b)
        }else{
        Section.EVENT_LISTENERS.ename=[b]
        }
    },
unbind:function(e){
    var c=Section.EVENT_LISTENERS.ename;
    if(!c){
        return
    }
    var d=0,b=c.length;
    for(d;d<b-1;d++){
        c[d]=c[d+1]
        }
        if(c.length>1){
        c.pop()
        }else{
        Section.EVENT_LISTENERS.ename=null
        }
        return
},
resize:function(b){
    var a=this.minHeight+this.yOffset;
    jQuery("body").css({
        height:(win.height()<a?a:win.height())+"px"
        })
    },
getCurrent:function(){
    return Section.CURRENT
    },
setCurrent:function(a){
    Section.CURRENT=a
    },
getFromID:function(a){
    return Section.COLLECTION[a]
    },
show:function(e){
    var b=this;
    console.log("Section::show",e);
    this.trigger(Section.SECTION_SHOWN,{
        currentTarget:this
    });
    if(this.sub_selected&&this.sub_selected.attr("rel")===e){
        return
    }
    var c=new RegExp(this._ID+"/([^/]+)","ig");
    var d=c.exec(e);
    if(d){
        var a=this.self.find('nav.sub-menu > ul > li > a[rel="/'+d[0]+'"]');
        a.trigger("dummyClick")
        }
        if(this.getCurrent()===this){
        return
    }
    this.self.css({
        top:(screen.height-200)+"px",
        display:"block"
    }).animate({
        top:this.yOffset+"px"
        },750,"easeOutExpo",function(){
        b.setCurrent(b)
        });
    this.resize()
    },
hide:function(){
    var a=this;
    Section.CURRENT=null;
    if(this.sub_selected){
        this.sub_selected.removeClass("selected");
        this.sub_selected=null
        }
        this.self.animate({
        top:(-this.self.height())+"px"
        },750,"easeOutExpo",function(){
        a.unbind(Section.SECTION_HIDDEN);
        a.trigger(Section.SECTION_HIDDEN,{
            currentTarget:a
        })
        })
    },
_onSubMenuHandler:function(c){
    var b=jQuery(c.currentTarget);
    var a=b.attr("rel");
    if(b.hasClass("selected")){
        return
    }
    if(c.type==="click"||c.currentTarget!==c.target){
        jQuery.address.value(a)
        }
        if(this.sub_selected){
        this.sub_selected.removeClass("selected")
        }
        this.sub_selected=$(c.currentTarget);
    this.sub_selected.removeClass("selected").addClass("selected");
    Cufon.replace("nav.sub-menu ul li a",{
        hover:"true"
    })
    }
});
var Banner=Objs("com.stilva.jwi.section.Banner","com.stilva.jwi.section.Section",{
    yOffset:0,
    banner:null,
    height:490,
    currentNb:0,
    swapInterval:0,
    innerInterval:0,
    initInterval:0,
    transform:null,
    initHover:false,
    IE7XOffset:0,
    initialize:function(c,b){
        Banner.$super.initialize.call(this,c);
        var a=this;
        this.minHeight=805;
        this.currentNb=Math.round(Math.random()*2);
        this.self=b;
        this.self.find("div.dotted-line").css({
            height:(50)+"px"
        }).find("div.black-block").last().css({
            top:"24px"
        });
        this.transform={
            WebkitTransform:"-webkit-transform",
            MozTransform:"-moz-transform",
            OTransform:"-o-transform",
            msTransform:"-ms-transform",
            transform:"transform"
        }
    },
attachListeners:function(){
    var a=this;
    this.swapInterval=setInterval(function(){
        a.initAnimation()
        },10000)
    },
removeListeners:function(){
    console.log("removeLIsteners called");
    clearInterval(this.swapInterval);
    clearInterval(this.innerInterval);
    clearInterval(this.initInterval)
    },
init:function(){
    this.dispImages()
    },
dispImages:function(){
    var d=2,b=3,c;
    var a=["left","middle","right"];
    for(;d>=0;d--){
        c='<div class="img-menu"><h1>'+ images[d][2];
				c+="</h1><h2>"+images[d][3];
				c+='</h2></div><img class="bw-image" src="'+images[d][0];
				c+='"><img class="big-image" src="'+images[d][1]+'">';
        jQuery("section#main div.wrapper div#img-"+(d+1)).append(c)
        }
        // Cufon.replace("h1, h2",{
        // fontFamily:"avenir"
    //})
    },
show:function(){
    if(this.getCurrent()&&this.getCurrent()._ID==="main"){
        return
    }
    Banner.$super.show.call(this,0);
    this.removeListeners();
    this.attachListeners();
    var a=this;
    this.initInterval=setInterval(function(){
        console.log("show initAnimation() called");
        a.initAnimation()
        },this.initHover?1500:2500)
    },
hide:function(){
    Banner.$super.hide.call(this);
    jQuery("div#animation-container").html("");
    this.removeListeners()
    },
initAnimation:function(){
    clearInterval(this.initInterval);
    var d=this,e=(this.currentNb+1===3)?0:this.currentNb+1;
    currentPics=[_banner_json[this.currentNb].left.bwImage,_banner_json[this.currentNb].middle.bwImage,_banner_json[this.currentNb].right.bwImage];
    newPics=[_banner_json[e].left.bwImage,_banner_json[e].middle.bwImage,_banner_json[e].right.bwImage];
    var c=jQuery('<div id="rotating-content"></div>').appendTo(jQuery('<div id="tmp-container"></div>').appendTo("div#animation-container"));
    var b=0,a=currentPics.length;
    for(;b<a;b++){
        c.append("<div "+(b===0?'id="first-facet"':"")+'class="facet"><div class="left-facet"><img src="'+currentPics[b]+'"></div><div class="right-facet"><img src="'+newPics[b]+'"></div></div><div class="facet"><div class="left-facet"><img src="'+currentPics[b]+'"></div><div class="right-facet"><img src="'+newPics[b]+'"></div></div>')
        }
        jQuery("div.banner-image").css({
        visibility:"hidden"
    });
    if(jQuery.browser.version==="7.0"&&jQuery.browser.msie){
        this.IE7XOffset=-405;
        jQuery(".facet:odd div img").css({
            position:"absolute",
            left:"-162px"
        })
        }
        jQuery(".facet").each(function(f){
        jQuery(this).css({
            left:(f*162+d.IE7XOffset)+"px",
            top:(f*-528)+"px"
            })
        });
    jQuery(".facet div.right-facet").css({
        zIndex:-777
    });
    if(Modernizr.csstransforms3d){
        this.swap3DImages()
        }else{
        this.swapImages()
        }
    },
swapImages:function(){
    var b=1,a=this;
    this.innerInterval=setInterval(function(){
        jQuery(".facet:nth-child("+b+") div.left-facet img").delay(b*100).animate({
            opacity:0
        },550,"easeInExpo",function(){
            jQuery(this).css("visibility","hidden")
            });
        b++;
        if(b===7){
            clearInterval(a.innerInterval);
            setTimeout(a.imagesSwapped,1100)
            }
        },165)
},
swap3DImages:function(){
    var b=1,a=this;
    this.innerInterval=setInterval(function(){
        jQuery(".facet:nth-child("+b+")").css({
            visibility:"visible"
        });
        jQuery(".facet:nth-child("+b+") div.left-facet").css(a.transform[Modernizr.prefixed("transform")],"rotateY(180deg)");
        jQuery(".facet:nth-child("+b+") div.right-facet").css(a.transform[Modernizr.prefixed("transform")],"rotateY(0deg)");
        b++;
        if(b===7){
            clearInterval(a.innerInterval);
            setTimeout(a.imagesSwapped,650)
            }
        },165)
},
imagesSwapped:function(){
    var a=window.banner;
    if(!a.initHover){
        a.self.find("div#img-1").bind("click mouseenter mouseleave",["img-1"],function(b){
            a._onBannerHandler(b)
            });
        a.self.find("div#img-2").bind("click mouseenter mouseleave",["img-2"],function(b){
            a._onBannerHandler(b)
            });
        a.self.find("div#img-3").bind("click mouseenter mouseleave",["img-3"],function(b){
            a._onBannerHandler(b)
            });
        a.initHover=true
        }
        window.banner.currentNb=(window.banner.currentNb+1===3)?0:window.banner.currentNb+1;
    jQuery("div.banner-image").css({
        visibility:"visible"
    });
    jQuery("section#main div.wrapper div#img-1").html("");
    jQuery("section#main div.wrapper div#img-2").html("");
    jQuery("section#main div.wrapper div#img-3").html("");
    window.banner.dispImages();
    jQuery("div#animation-container").animate({
        opacity:"0"
    },750,"easeInExpo",function(){
        jQuery(this).html("");
        jQuery(this).animate({
            opacity:"1"
        },0)
        })
    },
_onBannerHandler:function(h){
    var c=0,f=this;
    var d=jQuery(h.currentTarget);
    var i=d.find("img.bw-image").first();
    switch(h.type){
        case"click":
            var b,g=/\/([^\/]+)\/([^\/]+)\/?([\w-_]+)?/ig;
            if(this.banner!==null){
            switch(h.data[0]){
                case"img-1":
                    b=_banner_json[this.currentNb].left.slug;
                    break;
                case"img-2":
                    b=_banner_json[this.currentNb].middle.slug;
                    break;
                case"img-3":
                    b=_banner_json[this.currentNb].right.slug;
                    break
                    }
                    var a=g.exec(unescape(b));
            if(a){
                window.menu._onMouseHandler({
                    type:"click",
                    currentTarget:{
                        offsetParent:{
                            id:"#menu-"+a[1]
                            }
                        },
                preventDefault:function(){}
                },a[0])
        }
        }
    break;
case"mouseenter":
    if(f.banner){
    c=350
    }
    f.banner=d;
this.removeListeners();
    d.css("z-index",77);
    i.stop(true).animate({
    opacity:0,
    "z-index":77
},350);
d.stop(true).delay(350+c).animate({
    width:648
},350,function(){
    f._onBannerImageShown(d.find("div.img-menu"))
    });
break;
case"mouseleave":
    this.removeListeners();
    this.attachListeners();
    f._onBannerImageHide(d.find("div.img-menu"));
    d.stop(true).animate({
    width:324
},350);
i.stop(true).delay(350).animate({
    opacity:1,
    "z-index":7
},350,function(){
    d.css("z-index",7);
    f.banner=(f.banner&&f.banner.attr("id")===d.attr("id"))?null:f.banner
    });
break
}
},
_onBannerImageShown:function(c){
    c.parent().css({
        cursor:"pointer"
    });
    var b=[c.find("h1"),c.find("h2")];
    for(var a=0;a<2;a++){
        b[a].stop(true).css({
            display:"block",
            opacity:"0"
        }).animate({
            opacity:1
        },125).animate({
            "line-height":b[a].css("font-size")
            },125,"easeOutExpo")
        }
    },
_onBannerImageHide:function(c){
    c.parent().css({
        cursor:"auto"
    });
    var b=[c.find("h1"),c.find("h2")];
    for(var a=0;a<2;a++){
        b[a].stop(true).animate({
            "line-height":"50px"
        },125).animate({
            opacity:0
        },125,function(){
            jQuery(this).css({
                display:"none",
                opacity:"0"
            })
            })
        }
    }
});